import java.util.HashSet;
import java.util.Set;

/**
 * 加减法口算练习系统 - v0.3
 * Calculation_System_V0_3.java
 */
public class Calculation_System_V0_3 {
    
    public static void main(String[] args) {
        System.out.println("加减法口算练习系统 v0.3");
        
        // 生成50道混合加减法题
        generateExerciseOfMixedEquations();
    }
    
    // 生成50道混合加减法练习题
    static void generateExerciseOfMixedEquations() {
        System.out.println("--- 程序输出50道100以内的加减法混合口算题 ---");
        Set<String> usedEquations = new HashSet<>(); // 用于存储已使用的算式，避免重复
        int count = 0;
        
        while (count < 50) {
            // 随机选择加法或减法（各50%概率）
            boolean isAddition = Math.random() > 0.5;
            String equation;
            int n, m, result;
            
            if (isAddition) {
                // 生成加法题
                do {
                    n = generateOperand();
                    m = generateOperand();
                    result = n + m;
                } while (result >= 100); // 加法算式的和不能超过100
                equation = n + "+" + m;
            } else {
                // 生成减法题
                do {
                    n = generateOperand();
                    m = generateOperand();
                    result = n - m;
                } while (result < 0); // 减法算式的差不能小于0
                equation = n + "-" + m;
            }
            
            // 检查算式是否重复
            if (!usedEquations.contains(equation)) {
                usedEquations.add(equation);
                
                // 每行整齐地显示5个算式
                if (isAddition) {
                    System.out.printf("%2d+%2d=%-3d", n, m, result);
                } else {
                    System.out.printf("%2d-%2d=%-3d", n, m, result);
                }
                count++;
                
                // 每5个算式换一行
                if (count % 5 == 0) {
                    System.out.println();
                } else {
                    System.out.print("  "); // 算式之间的间距
                }
            }
        }
        
        // 统计加法和减法的数量
        int additionCount = 0;
        int subtractionCount = 0;
        for (String eq : usedEquations) {
            if (eq.contains("+")) {
                additionCount++;
            } else {
                subtractionCount++;
            }
        }
        
        System.out.println("\n题目统计：加法 " + additionCount + " 道，减法 " + subtractionCount + " 道");
    }
    
    // 生成100以内的整数操作数
    static int generateOperand() {
        return (int)(Math.random() * 100) + 1; // 生成1-100的随机数
    }
}